package com.fengniao.goods.service.impl;

import com.fengniao.goods.dao.TemplateMapper;
import com.fengniao.goods.dao.TemplateMapper;
import com.fengniao.goods.pojo.Template;
import com.fengniao.goods.pojo.Template;
import com.fengniao.goods.service.TemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author lanhai
 * @date 2021/7/5 17:01
 */
@Service
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    private TemplateMapper templateMapper;

    /**
     * 条件查询代码抽取
     * @param template
     * @return
     */
    public Example getExample(Template template) {
        Example example = new Example(Template.class);
        Example.Criteria criteria = example.createCriteria();

        if (template != null) {
            if (!StringUtils.isEmpty(template.getId())) {
                criteria.andEqualTo("id", template.getId());
            }
            if (!StringUtils.isEmpty(template.getName())) {
                criteria.andLike("name", "%" + template.getName() + "%");
            }
            if (!StringUtils.isEmpty(template.getSpecNum())) {
                criteria.andEqualTo("spec_num", template.getSpecNum());
            }
            if (!StringUtils.isEmpty(template.getParaNum())) {
                criteria.andEqualTo("para_num", template.getParaNum());
            }
        }
        return example;
    }

    @Override
    public PageInfo<Template> findPage(Template template, Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Template> Templates = findList(template);
        return new PageInfo<>(Templates);
    }

    @Override
    public PageInfo<Template> findPage(Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Template> Templates = templateMapper.selectAll();
        return new PageInfo<>(Templates);
    }

    @Override
    public List<Template> findList(Template template) {

        Example example = getExample(template);
        return templateMapper.selectByExample(example);
    }

    @Override
    public List<Template> findAll() {
        return templateMapper.selectAll();
    }

    @Override
    public Template findById(Integer id) {
        return templateMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(Template template) {
        return templateMapper.insertSelective(template);
    }

    @Override
    public int revise(Template template) {
        return templateMapper.updateByPrimaryKeySelective(template);
    }

    @Override
    public int remove(Integer id) {
        return templateMapper.deleteByPrimaryKey(id);
    }
}
